<?php

class Application_Form_EditUser extends Zend_Form
{

    public function init()
    {
        $this->setMethod('post')
        	->setAttrib('id', 'addUserForm');
        
        $this->clearDecorators();
        
        $decorators = array(
        		array('ViewHelper'),
        		array('Errors'),
        		array('Label', array('requiredSuffix' => ' *', 'class' => 'leftalign')),
        		array('HtmlTag', array('tag' => 'li')),
        );
        
        $id = $this->addElement('hidden', 'id');
        $id = $this->getElement('id')
        		->addFilter('Int');
        
        $email = $this->addElement('text', 'email_address',
        		array('label' => 'Email Address'));
        $email = $this->getElement('email_address')
        		->addValidator('EmailAddress')
        		->setRequired(true)
        		->addFilter('StringTrim');
        $email->getValidator('EmailAddress')->setMessage('Invalid email address');
        $email->setDecorators($decorators);
        
        $firstName = $this->addElement('text', 'first_name', 
        		array('label' => 'First Name'));
        $firstName = $this->getElement('first_name')
        		->addValidator('Alpha', false, array('allowWhiteSpace' => true))
        		->addFilter('StringTrim');
        $firstName->getValidator('Alpha')
        		->setMessage('Invalid first name. Must contain alphabetic characters only');
        $firstName->setDecorators($decorators);
        
        $lastName = $this->addElement('text', 'last_name',
        		array('label' => 'Last Name'));
        $lastName = $this->getElement('last_name')
        		->addValidator('Alpha', false, array('allowWhiteSpace' => true))
        		->addFilter('StringTrim');
        $lastName->getValidator('Alpha')
        		->setMessage('Invalid last name. Must contain alphabetic characters only');
        $lastName->setDecorators($decorators);
        
        $role = $this->addElement('select', 'role',
        		array(
        			'label' => 'Role',
        			'value' => 'member',
        			'multiOptions' => array(
        					'member' => 'Member',
        					'admin' => 'Admin'
        			)
        		)
        );
        $role = $this->getElement('role');
        $role->setDecorators($decorators);
        
        
        $submit = $this->addElement('submit', 'save_user',
        		array('label' => 'Save User'));
        $submit = $this->getElement('save_user')
        		->setDecorators(array(
        				array('ViewHelper'),
        ));
        
        $cancel = $this->addElement('submit', 'cancel',
        		array('label' => 'Cancel'));
        $cancel = $this->getElement('cancel')
        		->setDecorators(array(
        				array('ViewHelper'),
        ));
        
        $this->addDisplayGroup(array('save_user', 'cancel'), 'submit_buttons', 
        		array('disableLoadDefaultDecorators' => true));
        $submitButtons = $this->getDisplayGroup('submit_buttons');
        $submitButtons->addDecorators(array(
        				array('FormElements', 'Form'),
        				array('HtmlTag', array('tag' => 'li', 'class' => 'submit')),
        ));
        
        $this->setDecorators(array(
        		'FormElements',
        		array('HtmlTag', array('tag' => 'ul')),
        		array(array('DivTag' => 'HtmlTag'),
        			array('tag' => 'div', 'id' => 'saveUserDiv')),
        		'Form'
        ));
    }
    
    public function isValid($data) {
    	$email = $this->getElement('email_address')
	    	->addValidator('Db_NoRecordExists', true,
	    			array('table' => 'users', 'field' => 'email_address',
	    					'exclude' => array('field' => 'id', 'value' => $data['id'])));
    	$email->getValidator('Db_NoRecordExists')
    		->setMessage('Email address already exists');
    	return parent::isValid($data);
    }
}

